/**
 * Created on May 2, 2007
 */
package jvs.vfs.local;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import jvs.vfs.IFileBaseImpl;

/**
 * @author qiangli
 * 
 */
public class LocalFileImpl extends IFileBaseImpl {
	private final static int BUFSIZE = 1024;

	private java.io.File file = null;

	public LocalFileImpl(URI uri) {
		super(uri);
	}

	synchronized private java.io.File getFile() {
		if (file == null) {
			file = new java.io.File(uri);
		}
		return file;
	}
	
	public boolean canRead() {
		return getFile().canRead();
	}

	public boolean canWrite() {
		return getFile().canWrite();
	}

	public boolean copy(URI uri) {
		java.io.InputStream in;
		try {
			in = new java.io.FileInputStream(getFile());
			OutputStream out = uri.toURL().openConnection().getOutputStream();
			byte buf[] = new byte[BUFSIZE];
			int len;
			while ((len = in.read(buf, 0, BUFSIZE)) != -1) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return true;
		} catch (Exception e) {
			debug(e);
		}
		return false;
	}

	public boolean create() {
		try {
			return getFile().createNewFile();
		} catch (IOException e) {
			debug(e);
		}
		return false;
	}

	public boolean delete() {
		return getFile().delete();
	}

	public boolean exists() {
		return getFile().exists();
	}

	public String getContent() {
		try {
			int size = (int) getFile().length();
			byte[] buf = new byte[size];
			java.io.InputStream is;
			is = new java.io.FileInputStream(getFile());
			java.io.BufferedInputStream bis = new java.io.BufferedInputStream(
					is);
			int read = 0;
			for (int i = 0; i < getFile().length(); i += read) {
				read = bis.read(buf, i, buf.length - i);
			}
			return new String(buf);
		} catch (Exception e) {
			debug(e);
		}
		return null;
	}

	public String getContentEncoding() {
		return null;
	}

	public String getContentType() {
		return null;
	}

	public InputStream getInputStream() {
		try {
			return new java.io.FileInputStream(getFile());
		} catch (Exception e) {
			throw  new RuntimeException(e.getMessage()+": "+uri);
		}
	}

	public long getLastModified() {
		return getFile().lastModified();
	}

	public long getLength() {
		if (!getFile().exists()) {
			return -1;
		}
		return getFile().length();
	}

	public OutputStream getOutputStream() {
		try {
			return new java.io.FileOutputStream(getFile());
		} catch (Exception e) {
			debug(e);
		}
		return null;
	}

	public boolean isDirectory() {
		return getFile().isDirectory();
	}

	public boolean isFile() {
		return getFile().isFile();
	}

	public boolean link(URI uri) {
		return false;
	}

	public String[] list() {
		return getFile().list();
	}

	public boolean mkdir() {
		return getFile().mkdir();
	}

	public boolean move(URI dest) {

		if (dest.getScheme().equalsIgnoreCase("file")) {
			return getFile().renameTo(new java.io.File(dest));
		}
		return false;
	}

	public boolean setAttribute(String name, Object value) {
		return false;
	}

	public boolean setContent(String s) {
		try {
			java.io.FileWriter out = new java.io.FileWriter(getFile());
			out.write(s);
			out.close();
			return true;
		} catch (Exception e) {
			debug(e);
		}
		return false;
	}

	public boolean setContentEncoding(String encoding) {
		return false;
	}

	public boolean setContentType(String mimeType) {
		return false;
	}

	public boolean setInputStream(InputStream in) {
		try {
			OutputStream out = new java.io.FileOutputStream(getFile());
			byte buf[] = new byte[BUFSIZE];
			int len;
			while ((len = in.read(buf, 0, BUFSIZE)) != -1) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return true;
		} catch (Exception e) {
			debug(e);
		}
		return false;
	}

	public boolean setLastModified(long time) {
		return getFile().setLastModified(time);
	}

	public boolean setLength(long len) {
		return false;
	}

	public boolean setOutputStream(OutputStream out) {
		try {
			java.io.InputStream in = new java.io.FileInputStream(getFile());
			byte buf[] = new byte[BUFSIZE];
			int len;
			while ((len = in.read(buf, 0, BUFSIZE)) != -1) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return true;
		} catch (Exception e) {
			debug(e);
		}
		return false;
	}

	public void deleteOnExit() {
		getFile().deleteOnExit();
	}
}
